package model;

public class Fourmi extends Individu {
//test
	private Role role = new SansRole();
	private int duree_vie = 2;// 4320;
	private int mange = 0;
	private EnumStade stade = EnumStade.OEUF;
	private int age = 0;
	boolean etreVivant = true;
	Fourmiliere maFourmiliere;

	public Fourmi(Fourmiliere maFourmiliere) {
		this.maFourmiliere = maFourmiliere;
		//x = maFourmiliere.getX() + maFourmiliere.getTaille() / 2;
		//y = maFourmiliere.getY() + maFourmiliere.getTaille() / 2;
		x = (int) (Math.random() * maFourmiliere.getTaille());
		x = (int) (10*Math.floor(x/10));
		y = (int) (Math.random() * maFourmiliere.getTaille());
		y = (int) (10*Math.floor(y/10));
	}

	public Fourmi(Role role) {
		this.role = role;
		this.duree_vie = 10;// (int) (role.getDuree_vie_min() + (Math.random()
							// *
							// role
		// .getDuree_vie_max()));
		this.poids = (int) (role.getPoids_min() + (Math.random() * role
				.getPoids_max()));
		this.mange = poids / 3;
		this.stade = EnumStade.ADULTE;
		this.maFourmiliere = role.getFourmiliere();

	}

	public void vivre() {
		if (etreVivant) {
			if (age < duree_vie) {
				role.vivre(age);
				age++;
				deplacement();     // fourmis se deplacent de x-10 ...
			} else {
				evolution();
			}
		} else {
			meurt();
		}
	}

	public void meurt() {
		maFourmiliere.removeFourmi(this);
		// creer un cadavre
	}

	public void deplacement() {
		int versOu = 1 + (int) (Math.random() * 4);

		switch (versOu) {
		case 1:
			allerEnHaut();
			break;
		case 2:
			allerEnBas();
			break;
		case 3:
			allerADroite();
			break;
		case 4:
			allerAGauche();
			break;
		}
	}

	public void allerEnHaut() {
		if (y - Model.getTailleCase() > 0) {
			this.y = y - Model.getTailleCase();
		}
	}

	public void allerEnBas() {
		if (y + Model.getTailleCase() < 400 + maFourmiliere.getTaille()) {
			this.y = y + Model.getTailleCase();
		}
	}

	public void allerAGauche() {
		if (x - Model.getTailleCase() > 0) {
			this.x = x - Model.getTailleCase();
		}
	}

	public void allerADroite() {
		if (x + Model.getTailleCase() < 400 + maFourmiliere.getTaille()) {
			this.x = x + Model.getTailleCase();
		}
	}

	public void evolution() {
		switch (stade) {
		case OEUF:
			stade = EnumStade.LARVE;
			age = 0;
			duree_vie = 2;// 14400;
			poids = (int) (45 + (Math.random() * 80));
			mange = poids;
			break;

		case LARVE:
			stade = EnumStade.NYMPHE;
			age = 0;
			duree_vie = 2;// (int) (21600 + (Math.random() * 28800));
			poids = 0;
			mange = 0;
			break;

		case NYMPHE:
			stade = EnumStade.ADULTE;
			age = 0;
			duree_vie = 2;// (int)(788400+(Math.random()*1314000));
			poids = (int) (15 + (Math.random() * 20));
			mange = poids / 3;
			role = definirRole();
			break;

		case ADULTE:
			etreVivant = false;
			break;

		default:
			break;
		}
	}

	public Role definirRole() {
		int result = (int) (1 + (Math.random() * 120));
		if (result < 15) {
			Model.sex++;
			return new Indsexue();
		} else if (result < 41) {
			Model.sol++;
			return new Soldat();
		} else {
			Model.ouv++;
			return new Ouvriere();
		}
	}

	public Role getRole() {
		return role;
	}

	public EnumStade getStade() {
		return stade;
	}

	public void setStade(EnumStade stade) {
		this.stade = stade;
	}
	
	public String toString(){
		return "FOURMI: " + role + " (" + stade + ")" + " [" + x + "," + y + "]";
	}

}
